package com.iris.live.services.report.data.carmodelline;

import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.ReportDataConvertor;
import com.iris.live.services.report.data.formulars.FormularFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wangzhenyan
 * date: 2017/8/31 18:52
 */
@Component("CarModellineConvertor")
public class CarModellineConvertor extends ReportDataConvertor<CarModellineContext>{

    /**
     * 获取数据（非公式）
     * @param context
     * @param reportRow
     * @param resultSet
     * @param config
     * @throws Exception
     */
    @Override
    protected void getData(CarModellineContext context, Map reportRow, Map resultSet, ReportConfig config) throws Exception {
        Map resultCell = null;
        String valueKey = config.getValueKey(), fullPath = this.getFullPath(config);
        Object value = null;

        List<CarModelUnit> units = context.getUnits();
        if(config.isBlank()) {
            for (CarModelUnit unit : units) {
                resultCell = (Map) reportRow.get(fullPath + "_" + unit.getValue());
                if (resultCell == null) {
                    resultCell = new HashMap<>();
                    resultCell.put("color", 0);
                    resultCell.put("displayRow", unit.getValue());
                    resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                    resultCell.put(config.getCategory() + "Value", null);
                }
                reportRow.put(fullPath + "_" + unit.getValue(), resultCell);
            }
        }

        if(! resultSet.containsKey(valueKey)){
            resultCell = new HashMap();
            resultCell.put("color", 0);
            this.setResultCell(resultCell, config, 0);
            return;
        }
        if(resultSet.get(valueKey) != null){
            value = resultSet.get(valueKey).toString();
        }else{
            value = 0;
        }

        //处理累计
        for (CarModelUnit unit : units) {
            resultCell = (Map) reportRow.get(fullPath + "_" +unit.getValue());
            if (resultCell == null) {
                resultCell = new HashMap<>();
                resultCell.put("color", 0);
                resultCell.put("displayRow", unit.getValue());
                resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                resultCell.put(config.getCategory()+ "Value",Double.valueOf(0));
            }
            this.setResultCell(resultCell, config, 0);
            reportRow.put(fullPath + "_" +unit.getValue() , resultCell);
        }

        for (CarModelUnit unit : units) { // 按照设置的车型x轴维度进行合计
            // 总计
            if (unit.getType() == CarModelUnit.CarModelType.TOTAL) {
                String unitValue = unit.getValue();
                resultCell = (Map) reportRow.get(fullPath + "_" +unitValue);
                if (resultCell == null) {
                    resultCell = new HashMap<>();
                    resultCell.put("color", 0);
                    resultCell.put("displayRow", unitValue);
                    resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                }
                this.setResultCell(resultCell, config, value);
                reportRow.put(fullPath + "_" +unitValue , resultCell);
            }
            // 品牌总计、车系总计、车型
            if (unit.getType() == CarModelUnit.CarModelType.BRAND || unit.getType() == CarModelUnit.CarModelType.SERIES || unit.getType() == CarModelUnit.CarModelType.MODEL) {
                String unitValue = unit.getValue(),dbValue = resultSet.get(unit.getColumn()).toString();
                if (unitValue.equals(dbValue)) {
                    resultCell = (Map) reportRow.get(fullPath + "_" +unitValue);
                    if (resultCell == null) {
                        resultCell = new HashMap<>();
                        resultCell.put("color", 0);
                        resultCell.put("displayRow", unitValue);
                        resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                    }
                    this.setResultCell(resultCell, config, value);
                    reportRow.put(fullPath + "_" +unitValue , resultCell);
                }
            }
        }


    }

    /**
     * 获取公式数据
     * @param context
     * @param datas
     * @param reportRow
     * @param config
     * @throws Exception
     */
    @Override
    protected void getFormularData(CarModellineContext context, Map<String, Map> datas, Map reportRow, ReportConfig config) throws Exception {
        String fullPath = this.getFullPath(config), formularExpression = config.getFormularExpression();
        Map resultCell = null;
        String formular = formularExpression.substring(0, formularExpression.indexOf("("));
        List<CarModelUnit> units = context.getUnits();
        for(CarModelUnit unit : units){ //按照用户设置的时间轴上的时间和当前时间进行比较并进行合计
            resultCell = (Map) reportRow.get(fullPath + "_" + unit.getValue());

            if (resultCell == null) {
                resultCell = new HashMap<>();
                resultCell.put("color", 0);
                resultCell.put("displayRow", unit.getValue());
                resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                resultCell.put(config.getCategory() + "Value", 0);
            }
            Object value = getData(formular, config, unit, datas);
            this.setResultCell(resultCell, config, value);
            reportRow.put(fullPath + "_" + unit.getValue(), resultCell);
        }
    }

    /**
     * 获取公式项值
     * @param formular
     * @param config
     * @param unit
     * @param datas
     * @return
     * @throws Exception
     */
    protected Double getData(String formular, ReportConfig config, CarModelUnit unit, Map<String, Map> datas) throws Exception{
        return  FormularFactory.getFormular(formular).getValue(config, unit.getValue(), datas);
    }

}
